package it.hopeless.jee6.conversation;

import it.hopeless.jee6.decorator.Greeting;
import it.hopeless.jee6.decorator.Simple;
import it.hopeless.jee6.interceptor.SortAction;

import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.Collections;
import java.util.List;

@Stateful
@Named("sorter")
@ConversationScoped
public class SortLetter{

    @Inject
    Conversation conversation;

    @Inject @SaveLetter Collector collector;

    @Inject @Sorted
    Event<String> event;

    @Inject @Simple
    Greeting greet;
    @PostConstruct
    public void start(){
        conversation.begin();
    }

    public Greeting getGreet() {
        return greet;
    }

    public void setText(String text){
        collector.setText(text);
    }

    public String getText(){
        return collector.getText();
    }

    public Collector getCollector(){
        return collector;
    }


    @SortAction
    public void sort(){
        event.fire(collector.getLettersText());
        collector.sort();
        conversation.end();
    }

}
